import { useLaunch } from '@tarojs/taro'
import React, { useState, useEffect } from 'react'
import { Provider } from 'react-redux'
import { AuthProvider } from '../contexts/AuthContext'
import store from '../store'
import ErrorBoundary from './ErrorBoundary'

function App({ children }) {
  // 保存React节点的引用状态
  const [appMounted, setAppMounted] = useState(false)

  // 处理应用挂载
  useEffect(() => {
    setAppMounted(true)
    return () => {
      // 确保卸载前清理
      setAppMounted(false)
    }
  }, [])

  useLaunch(() => {
    console.log('App launched.')
  })

  // 使用Provider包裹应用，提供Redux store，再用AuthProvider包裹，添加错误边界
  return (
    <Provider store={store}>
      <AuthProvider>
        <ErrorBoundary>
          {children}
        </ErrorBoundary>
      </AuthProvider>
    </Provider>
  )
}

export default App